<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Report extends CI_Controller {
 	public function __construct()
    {
        parent::__construct();
        session_start();
    	if (!isset($_SESSION['username'])){
	    	redirect('administration','refresh');
	    }
        elseif($_SESSION['username']=='dara')
        {
            redirect('itstock','refresh');
        }
        $this->load->model('MCategory');
        $this->load->model('MStock');
        $this->load->model('MStatic');
	    $this->load->helper('csv');
    }
	    
	public function index()
	{
		$data['title'] = "Dashboard - Stock Inventory System";
		$data['main'] = 'dashboard';
		$this->load->vars($data);
		$this->load->view('layout'); 
	}

    public function itemList($id)
    {
        $title = '';
        $items = null;
        $file = null;
        if($id == 1){
            $title = "Items in Stock Inventory System";
            $items = $this->MStock->getInstockItem(1);
            $file = 'report/instock';
        }
        elseif($id == 2){
            $title = "Used Items in Inventory";
            $items = $this->MStock->getReportOutStock();
            $file = 'report/useditem';
        }
        elseif($id == 3){
            $title = "Broken Items in Stock Inventory System";
            $items = $this->MStock->getInstockItem(3);
            $file = 'report/instock';
        }
        $data['status'] = $id;
        $data['title'] = $title;
		$data['main'] = $file;
        $data['items'] = $items->result();
        $data['categoryList'] = $this->MCategory->getCategoryDropdown();
        $data['branchList'] = $this->MStatic->getBranchDropdown();
        $data['category_id'] = '';
        $data['datestart'] = '';
        $data['dateend'] = '';
        $data['branch'] = '';
		$this->load->vars($data);
		$this->load->view('layout'); 
    }
    
    public function searchItem()
    {
        $id = $this->input->post('status_id');
        $start = $this->input->post('datestart');
        $end = $this->input->post('dateend');
        $category_id = $this->input->post('category_id');
        $branch = $this->input->post('branch');
        if($start || $end || $category_id > 0 || $branch)
        {
            $title = '';
            $items = null;
            $file = null;
            if($id == 1){
                $title = "Items in Stock Inventory System";
                $items = $this->MStock->getInstockItem(1,$start,$end, $category_id, $branch);
                $file = 'report/instock';
            }
            elseif($id == 2){
                $title = "Used Items in Inventory";
                $items = $this->MStock->getReportOutStock($start, $end, $category_id, $branch);
                $file = 'report/useditem';
            }
            elseif($id == 3){
                $title = "Broken Items in Stock Inventory System";
                $items = $this->MStock->getInstockItem(3,$start,$end, $category_id, $branch);
                $file = 'report/instock';
            }
            $data['status'] = $id;
            $data['title'] = $title;
    		$data['main'] = $file;
            $data['items'] = $items->result();
            $data['categoryList'] = $this->MCategory->getCategoryDropdown();
            $data['category_id'] = $this->input->post('category_id');
            $data['branchList'] = $this->MStatic->getBranchDropdown();
            $data['datestart'] = $start;
            $data['dateend'] = $end;
            $data['branch'] = $branch; 
    		$this->load->vars($data);
    		$this->load->view('layout'); 
        }else{
            redirect('report/itemlist/'.$id,'refresh');
        }
    }
  
    public function exportItem()
    {
        $id = $this->input->post('status_id');
        $start = $this->input->post('datestart');
        $end = $this->input->post('dateend');
        $category_id = $this->input->post('category_id');
        $branch = $this->input->post('branch');
        if($id == 1){
            $items = $this->MStock->getInstockItem(1,$start,$end, $category_id, $branch);
            query_to_csv($items,TRUE,'In Stock_'.date('dMy').'.csv');
        }
        elseif($id == 2){
            $items = $this->MStock->getReportOutStock($start, $end, $category_id, $branch);
            query_to_csv($items,TRUE,'Used_'.date('dMy').'.csv');
        }
        elseif($id == 3){
            $items = $this->MStock->getInstockItem(3,$start,$end, $category_id, $branch);
            query_to_csv($items,TRUE,'Broken Items_'.date('dMy').'.csv');
        }
    }
    
    public function test()
    {
        $this->MStock->addEditStock(id_clean($this->input->post('stock_id')));
    }
}
